package nciae.db.controller;

import lombok.extern.slf4j.Slf4j;

import nciae.db.common.CommonResult;
import nciae.db.pojo.entity.Classs;
import nciae.db.pojo.entity.Student;
import nciae.db.service.ClassService;
import nciae.db.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/class")
@Slf4j
public class ClassController {

    @Autowired
    private ClassService classService;

    @Autowired
    private StudentService studentService;


    // 新增查询班级学员的接口
    @GetMapping("/{classId}/students")
    public CommonResult<List<Student>> getClassStudents(@PathVariable Long classId) {
        CommonResult<List<Student>> result = new CommonResult<>();
        try {
            List<Student> students = studentService.getStudentsByClassId(classId);
            result.setCode(0);
            result.setMsg("查询成功");
            result.setData(students);
            return result;
        } catch (Exception e) {
            result.setCode(-1);
            result.setMsg("查询失败: " + e.getMessage());
            return result;
        }
    }

    /**
     * 查询所有班级
     * @return
     */
    @GetMapping("/lists")
    public CommonResult<List<Classs>> getClassStudents() {
        CommonResult<List<Classs>> result = new CommonResult<>();
        try {
            List<Classs> students = classService.list();
            result.setCode(0);
            result.setMsg("查询成功");
            result.setData(students);
            return result;
        } catch (Exception e) {
            result.setCode(-1);
            result.setMsg("查询失败: " + e.getMessage());
            return result;
        }
    }

    @PostMapping("/del")
    public CommonResult del(Integer classId) {
        CommonResult result = new CommonResult<>();
        try {
            boolean delete = classService.removeClass(classId);
            if (delete) {
                result.setCode(0);
                result.setMsg("删除成功");
            } else {
                result.setCode(-1);
                result.setMsg("班级内还有成员，无法删除");
            }
            return result;
        } catch (Exception e) {
            result.setCode(-1);
            result.setMsg("查询失败: " + e.getMessage());
            return result;
        }
    }

    /**
     * 分页查询班级列表
     *
     * @param page
     * @param limit
     * @param className
     * @param chargerName
     * @param startDate
     * @param endDate
     * @return
     */
    @GetMapping("/list")
    public CommonResult page(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer limit,
                             @RequestParam(required = false) String className,
                             @RequestParam(required = false) String chargerName,
                             @RequestParam(required = false) String startDate,
                             @RequestParam(required = false) String endDate,
                             HttpServletRequest request) {
        log.info("分页查询班级参数:{} {} {} {} {}", page, limit, className, chargerName, startDate, endDate);
        log.info("startDate:"+startDate);
        log.info("endDate:"+endDate);

        // 调用 Service 层的查询方法
        CommonResult searchResult = classService.search(className, chargerName, startDate, endDate, page, limit, request);

        return searchResult;
    }

    /**
     * 查询班级详细信息
     *
     * @param classId
     * @return
     */
    @GetMapping("/detail")
    public CommonResult GetClassDetails(@RequestParam("classId") Integer classId) {
        log.info("classid" + classId);
        Classs classs = classService.getById(classId);
        CommonResult result = new CommonResult();
        result.setCode(0);
        result.setData(classs);
        return result;
    }

    /**
     * 修改班级信息
     *
     * @param classs
     * @return
     */
    @PutMapping("/update")
    public CommonResult updateClass(@RequestBody Classs classs) {
        log.info("修改班级信息: {}", classs);
        CommonResult result = new CommonResult();
        if (classs.getMonitorId() != null) {
            Student student = studentService.getById(classs.getMonitorId());
            classs.setMonitorName(student.getName());
        }
        boolean updateSuccess = classService.updateById(classs);
        if (updateSuccess) {
            result.setCode(0);
            result.setMsg("班级信息修改成功");
        } else {
            result.setCode(-1);
            result.setMsg("班级信息修改失败");
        }
        return result;
    }
//    @PostMapping("/{classId}/resetPassword")
//    public CommonResult<String> resetPassword(@PathVariable Long classId) {
//        classService.resetPasswordForClass(classId);
//        return
//    }

    @ResponseBody
    @GetMapping("/listClassName")
    public CommonResult<List<String>> listClassName() {
        return classService.listClassName();
    }
    @ResponseBody
    @GetMapping("/listChargerRelevance")
    public CommonResult<List<String>> listChargerRelevance(HttpServletRequest request) {
        String chargerName = request.getParameter("chargerName");
        String param=request.getParameter("param");
        return classService.listChargerRelevance(chargerName,param);
    }

    /**
     * 可以获取所有项目或班级，暂时没用……
     * @param request
     * @return
     */
    @ResponseBody
    @GetMapping("/listAllRelevance")
    public CommonResult<List<String>> listAllRelevance(HttpServletRequest request) {
        String param=request.getParameter("param");
        return classService.listAllRelevance(param);
    }
    @RequestMapping("/findClassNum")
    @ResponseBody
    public  CommonResult<Integer> findClassNum(HttpServletRequest request) {
        String className = request.getParameter("className");
        return classService.findClassNum(className);
    }
    @RequestMapping("/getProjectsByClass")
    @ResponseBody
    public  CommonResult<List<String>> getProjectsByClass(@RequestParam("class") String selectedClass) {
        return classService.getProjectsByClass(selectedClass);
    }
    @RequestMapping("/getClassesByProject")
    @ResponseBody
    public  CommonResult<List<String>> getClassesByProject(@RequestParam("project") String selectedProject,@RequestParam("chargerId") Integer chargerId) {
        return classService.getClassesByProject(selectedProject,chargerId);
    }
    @GetMapping("/{classId}/export")
    public void exportClassStudents(@PathVariable Integer classId, HttpServletResponse response) {

        log.info("classid:"+classId);
        try {
            classService.exportClassStudents(classId, response);
        } catch (Exception e) {
            log.error("导出班级成员失败", e);
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    }
}
